Fix skipping Jupyter cells with unknown %% magic #4462
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This CL fixes an inconsistency of when to skip formatting of Jupyter cells with unknown magic methods.
Black correctly skips formatting in this case:
However if the cell started with some empty lines black still tried to format it and failed:
Also if the cell started with a comment black tried to format it:
(This example isn't possible in normal Jupyter notebooks but it works in Colab.)
This PR ensures that black will skip formatting cells with a custom magic function which is preceded by any number of empty lines and lines with comments.
Additional notes:
I moved
validate_cell
function from__init__.py
tohandle_ipynb_magics.py
module because it belongs there. (It also makes my maintenance of Pyink fork a bit easier if there are less things in__init__.py
module.) But if there is a reason to keep it in__init__.py
I can move it back.Function
_get_code_start
could also be implemented like this:It might be a bit more efficient but it is also less readable. Let me know if you would prefer this implementation.
Checklist - did you ...
CHANGES.md
if necessary?Add new / update outdated documentation?